﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class DictTypeService : BaseService, IDictTypeService
    {
        private readonly IDictTypeRepository _dicttyperepository;
        private readonly IUnitOfWork _unitOfWork;
        public DictTypeService(IMapper mapper, IDictTypeRepository _dicttyperepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            this._dicttyperepository = _dicttyperepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddDictType(DictType m)
        {
            string code = "DictType";
            var aobj = _dicttyperepository.GetEntityWhereAll(x => x.TypeCode.Contains(code)).OrderByDescending(x => x.CreateDate).FirstOrDefault();
            if (aobj != null)
            {
                string pattern = @"\d+";
                MatchCollection matches = Regex.Matches(aobj.TypeCode, pattern);
                string value = matches[matches.Count - 1].Value;
                int values = Convert.ToInt32(value);
                values++;
                m.TypeCode = code + values.ToString("D4");
            }
            else
            {
                int i = 1;
                m.TypeCode = code + i.ToString("D4");
            }

            var tobj = _dicttyperepository.QueryEntityWhere(x => x.TypeCode == m.TypeCode);
            if (tobj != null)
            {
                return false;
            }

            _dicttyperepository.AddEntity(m);
            return _unitOfWork.SaveChangesEntity();
        }

        /// <summary>
        /// 删除字典类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelDicType(long id)
        {
            var dicobj = _dicttyperepository.QueryEntityById(id);
            if (dicobj != null)
            {
               _dicttyperepository.DelEntity(dicobj);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        /// <summary>
        /// 查询字典类型
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<DictType> GetAllDictTypeList(string? keyword, int pageIndex, int pageSize, out int totalCount)
        {
            var dicList = _dicttyperepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(keyword))
            {
                dicList = dicList.Where(c => c.TypeName.Contains(keyword));
            }
            var list = _dicttyperepository.GetEntityAlls(dicList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        /// <summary>
        /// 字典项左侧树形
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="isTree"></param>
        /// <returns></returns>
        public List<DictType> GetAllDictTypeTree(string? keyword, bool isTree)
        {
            var list = _dicttyperepository.GetEntityWhereAll(d => d.IsTree);
            if (!isTree)
            {
                list = _dicttyperepository.GetEntityWhereAll(d => d.IsTree == false);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(c => c.TypeName.Contains(keyword));
            }
            return list.ToList();
        }

        /// <summary>
        /// 根据主键获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public DictType? GetDictTypeByID(long id)
        {
            return _dicttyperepository.QueryEntityById(id);
        }

        /// <summary>
        /// 修改字典类型
        /// </summary>
        /// <param name="dictType"></param>
        /// <returns></returns>
        public bool UpdDicType(DictType dictType)
        {
            var dobj = _dicttyperepository.QueryEntityWhere(x => x.Id != dictType.Id && x.TypeCode == dictType.TypeCode);
            if (dobj != null)
            {
                return false;
            }    
            _dicttyperepository.UpdEntity(dictType);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
